<?php

/*
 * Description of book_tour_model
 * @author Pham Minh Khoi
 * @email: pmkhoi@gmail.com
 * @package:
 */


class Consultant_model extends CI_Model {

    //put your code here
    function __constructor() {
        parent::__construct();
    }

    function create($category) {

        $sql = "INSERT INTO `gtb_consultant` " .
                "(`fullname`, `address`, `email`, `phone`, `fax`, `title`, `question`, `answer`, " .
                "`position`, `status`, `active`, `add_date`) " .
                "VALUES ";

        $sql .= "(" .
                $this->db->escape($category['fullname']) . ", " .
                $this->db->escape($category['address']) . ", " .
                $this->db->escape($category['email']) . ", " .
                $this->db->escape($category['phone']) . ", " .
                $this->db->escape($category['fax']) . ", " .
                $this->db->escape($category['title']) . ", " .
                $this->db->escape($category['question']) . ", " .
                $this->db->escape($category['answer']) . ", " .
                ((int)$this->getMaxPosition() + 1). ", " .
                $this->db->escape($category['status']) . ", " .
                $this->db->escape($category['active']) . ", " .
                $this->db->escape($category['add_date']) .         
                ");";

        $this->db->query($sql);
        //return $this->db->affected_rows();
        return $this->db->insert_id();
    }

    function update($category) {

        $sql = "UPDATE `gtb_consultant` SET" .
                " fullname = " . $this->db->escape($category['fullname']). ",".
                " address = " . $this->db->escape($category['address']) . ", " .
                " email = " . $this->db->escape($category['email']) . ", " .
                " phone = " .  $this->db->escape($category['phone']) . ", " .
                " fax = " .  $this->db->escape($category['fax']) . ", " .
                " title = " . $this->db->escape($category['title']) . ", " .
                " question = " . $this->db->escape($category['question']) . ", " .
                " answer = '" . ($category['answer']) . "', " .
                " position = " . intval($category['position']) . ", " .
                " upd_date = " .$this->db->escape($category['upd_date']) . ", " .
                " active = " .intval($category['active']) . ", " .
                " status = ".  intval($category['status']) .                
                " WHERE id = " . intval($category['id']);

        $this->db->query($sql);
        return $this->db->affected_rows();
        //return $this->db->insert_id();
    }

    function delete($id) {
        $sql = "DELETE FROM gtb_consultant
                WHERE id = " . intval($id);
        $this->db->query($sql);

        $query = $this->db->query($sql);
        $result =$this->db->affected_rows();

        return $result;
    }

    function getAll($offset = 0, $limit = NUMBER_RECORD_PER_PRODUCT_PAGE, $para = '') {
        $sql = "SELECT * FROM gtb_consultant
                ORDER BY position DESC
                LIMIT " . intval($offset) . "," . intval($limit);
        $this->db->query($sql);

        $query = $this->db->query($sql);
        $result = $query->result_array();

        return $result;
    }
    
    function getAllFrontEnd($offset = 0, $limit = NUMBER_RECORD_PER_PRODUCT_PAGE, $active = -1) {
       $sql = "SELECT id as item_id, title as item_title
                FROM gtb_consultant
                WHERE (active = $active OR -1 = $active)
                ORDER BY position DESC
                LIMIT " . intval($offset) . "," . intval($limit);
        $this->db->query($sql);

        $query = $this->db->query($sql);
        $result = $query->result_array();

        return $result;
    }

    function countAll($para = '') {
        $sql = "SELECT count(*) as number_record
		FROM gtb_tour
		";
        $query = $this->db->query($sql);
        if ($result = $query->result()) {
            return $result[0]->number_record;
        }
    }

    function getMaxPosition() {
        $sql = "SELECT max(position) as max_position
		FROM gtb_consultant
		";
        $query = $this->db->query($sql);
        if ($result = $query->result()) {
            return $result[0]->max_position;
        }

        return 1;
    }

    function findByID($id) {
        $sql = "SELECT *
		FROM gtb_consultant
                WHERE id = " . intval($id);
        $query = $this->db->query($sql);
        if ($result = $query->result_array()) {
            return $result[0];
        }
        return null;
    }

    function getByFriendlyUrl($link_rewrite){

		$sql = "SELECT *
				FROM gtb_tour
				WHERE friendly_url = " . $this->db->escape($link_rewrite);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}

		return null;
	}

        function getByCode($code){

		$sql = "SELECT *
				FROM gtb_tour
				WHERE code = " . $this->db->escape($code);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}

		return null;
	}

        function setImage($image, $tourID){
            $sql = "UPDATE gtb_tour SET image = ". $this->db->escape($image).
                   " WHERE id = " . intval($tourID);
            $query = $this->db->query($sql);
            return $this->db->affected_rows();
        }
        

        function findByCategory($cid, $offset = 0, $limit = 10) {
        $sql = "SELECT *
		FROM gtb_tour
                WHERE tour_category_id IN( " . $cid .") AND (special = 1 OR best_sell = 1)
                ORDER BY position DESC
                LIMIT " . intval($offset) . "," . intval($limit);
        $query = $this->db->query($sql);
        if ($result = $query->result_array()) {
            return $result;
        }
    }
    
    function getNewest() {
        $sql = "SELECT *
		FROM gtb_consultant
                WHERE active = 1
                ORDER BY id DESC
                LIMIT 0,1 ";
        $query = $this->db->query($sql);
        if ($result = $query->result_array()) {
            return $result[0];
        }

        return null;
    }
}

?>
